﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ingr.SP3D.Common.Client.Controls;
using static SWS.PIP.CMD.CommonFunctions;
using Ingr.SP3D.Common.Middle;
using Ingr.SP3D.Common.Client.Services;
using Ingr.SP3D.Common.Middle.Services;
using TMPipeTopology;
using Ingr.SP3D.Common.Middle.Services.Hidden;
using System.Data.SqlClient;
using SWS.PIP.DM;
using Ingr.SP3D.Common;
using System.Data.Entity.Infrastructure;
using System.Collections;

namespace SWS.PIP.CMD
{
	public partial class PipeSpoolManangeForm : Form
	{
		#region
		//GraphicViewHiliter m_Hiliter = new GraphicViewHiliter();

		Dictionary<string, Dictionary<string, Dictionary<string, List<ParentAss>>>> m_MajorAndAreaMap = new Dictionary<string, Dictionary<string, Dictionary<string, List<ParentAss>>>>();
		Dictionary<string, SMgnt_SpoolInfo> m_OidAndSMgntSpoolMap = new Dictionary<string, SMgnt_SpoolInfo>();

		string m_sDrawNo = "";

		string m_sCurrentUser;

		ColorPanelForm m_ColorPanel = null;
		#endregion

		/// <summary>
		/// 初始化数据库连接，创建数据表等
		/// </summary>
		public PipeSpoolManangeForm()
		{
			InitializeComponent();

			//修改选项卡绘制方式
			tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
			tabControl.DrawItem += TabControl_DrawItem;

			//获取当前用户
			m_sCurrentUser = ProjectMgmtHelper.GetCurrentAccountUserName(true);

			//m_Hiliter.Weight = 4;
			//m_Hiliter.Color = ColorConstants.RGBYellow;

			try
			{
				//初始化数据库表格
				string sMDBName = MiddleServiceProvider.SiteMgr.ActiveSite.ActivePlant.PlantModel.Name;
				CommonFunctions.InitialSpoolMgntDatabase(sMDBName);
			}
			catch (System.Exception ex)
			{
				MessageBox.Show($"初始化数据库失败，错误：{ex.Message}");
				Close();
			}

			//从数据库中获取已有的区域分段节点信息
			using (SMgntEntities mdbContext = new SMgntEntities())
			{
				m_MajorAndAreaMap = CommonFunctions.GetAreaBlockAndParentAssMapFromDB(mdbContext);
				SetMajor();
			}
		}

		/// <summary>
		/// 控制tab选项卡颜色
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void TabControl_DrawItem(object sender, DrawItemEventArgs e)
		{
			string tabName = tabControl.TabPages[e.Index].Text;
			Brush bshBack = Brushes.White;

			//根据当前选择小票的问题状态，设置table标签颜色，仅在有一行数据被选中的情况下刷颜色，其他状态默认颜色
			if (dataGridView_SpoolInfo != null && dataGridView_SpoolInfo.SelectedRows.Count ==1)
			{
				string sRelatedTable;
				foreach (DataGridViewRow row in dataGridView_PipeIssue.Rows)
				{
					sRelatedTable = row.Cells[PipeIssueTable.RelatedTable].Value.ToString();

					if ((e.Index == 1 && sRelatedTable == "装配表") ||
						(e.Index == 2 && sRelatedTable == "弯管数据表") ||
						(e.Index == 3 && sRelatedTable == "校管表") ||
						(e.Index == 4 && sRelatedTable == "材料表") ||
						(e.Index == 5 && sRelatedTable == "焊点表"))
					{
						bshBack = Brushes.Pink;
						break;
					}
				}
			}

			//刷背景
			e.Graphics.FillRectangle(bshBack, e.Bounds);

			//刷字体
			Font fntTab = e.Font;
			if (e.Index == tabControl.SelectedIndex)
				fntTab = new Font(e.Font, FontStyle.Bold);
			else
			{
				if (e.Index == 0)
				{
					if (dataGridView_PipeIssue.RowCount == 0)
						fntTab = new Font(e.Font, FontStyle.Italic);
					else
						fntTab = new Font(e.Font, FontStyle.Regular);
				}
				else if (e.Index == 1)
				{
					if (dataGridView_Assembly.RowCount == 0)
						fntTab = new Font(e.Font, FontStyle.Italic);
					else
						fntTab = new Font(e.Font, FontStyle.Regular);
				}
				else if (e.Index == 2)
				{
					if (dataGridView_PipeBend.RowCount == 0)
						fntTab = new Font(e.Font, FontStyle.Italic);
					else
						fntTab = new Font(e.Font, FontStyle.Regular);
				}
				else if (e.Index == 3)
				{
					if (dataGridView_PipeCheck.RowCount == 0)
						fntTab = new Font(e.Font, FontStyle.Italic);
					else
						fntTab = new Font(e.Font, FontStyle.Regular);
				}
				else if (e.Index == 4)
				{
					if (dataGridView_PipePart.RowCount == 0)
						fntTab = new Font(e.Font, FontStyle.Italic);
					else
						fntTab = new Font(e.Font, FontStyle.Regular);
				}
				else if (e.Index == 5)
				{
					if (dataGridView_PipeWeld.RowCount == 0)
						fntTab = new Font(e.Font, FontStyle.Italic);
					else
						fntTab = new Font(e.Font, FontStyle.Regular);
				}
			}
			Rectangle recTab = e.Bounds;
			Brush bshFore = Brushes.Black;
			recTab = new Rectangle(recTab.X, recTab.Y + 4, recTab.Width, recTab.Height - 4);
			e.Graphics.DrawString(tabName, fntTab, bshFore, recTab);
		}

		/// <summary>
		/// 管理管小票表格右键菜单
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionShowSpoolContextMenu(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				if (e.RowIndex >= 0)
				{
					//设置行被选中（右键不会选中行）
					if (dataGridView_SpoolInfo.Rows[e.RowIndex].Selected == false)
					{
						dataGridView_SpoolInfo.ClearSelection();
						dataGridView_SpoolInfo.Rows[e.RowIndex].Selected = true;
						dataGridView_SpoolInfo.CurrentCell = dataGridView_SpoolInfo[e.ColumnIndex, e.RowIndex];
					}

					//弹出菜单
					SpoolListCtxMenu.Show(MousePosition.X, MousePosition.Y);

					//根据选择的行数，激活 或 取消激活更改页码菜单
					menuItem_ChangePageNumber.Enabled = true;
					menuItem_Delete.Enabled = true;
					menuItem_Update.Enabled = true;
					menuItem_ChangeToNull.Enabled = true;
					menuItem_Highlight.Enabled = true;
					menuItem_ConfirmStatus.Enabled = true;
					int iSelectVisibleRowCount = 0;
					foreach (DataGridViewRow row in dataGridView_SpoolInfo.SelectedRows)
					{
						if (row.Visible == false)
							continue;

						iSelectVisibleRowCount++;

						RowStatus status = (RowStatus)row.Cells[SpoolInfoTable.Status].Value;
						string sPageNo = row.Cells[SpoolInfoTable.PageNoDisplay].Value.ToString();

						//更新，高亮
						//if ((status& RowStatus.DELETE) == RowStatus.DELETE ||
						//	(status & RowStatus.NULL) == RowStatus.NULL)
						//{
						//	menuItem_Update.Enabled = false;
						//	menuItem_Highlight.Enabled = false;
						//}
						//删除
						if ((status & RowStatus.CHANGENODEFROM) == RowStatus.CHANGENODEFROM ||
							(status & RowStatus.NEW) == RowStatus.NEW)
						{
							menuItem_Delete.Enabled = false;
						}
						//if ((status & RowStatus.CHANGENODEFROM) == RowStatus.CHANGENODEFROM ||
						//	(status & RowStatus.CHANGENODETO) == RowStatus.CHANGENODETO ||
						//	(status & RowStatus.NEW) == RowStatus.NEW)
						//{
						//	menuItem_ChangePageNumber.Enabled = false;
						//}
						//留白
						if ((status & RowStatus.CHANGENODEFROM) == RowStatus.CHANGENODEFROM ||
							(status & RowStatus.NEW) == RowStatus.NEW ||
							(status & RowStatus.NULL) == RowStatus.NULL ||
							sPageNo == "")
						{
							menuItem_ChangeToNull.Enabled = false;
						}
						//状态确认
						if ((status & RowStatus.CHANGEATTR) == RowStatus.CHANGEATTR ||
							(status & RowStatus.CHANGENODEFROM) == RowStatus.CHANGENODEFROM ||
							(status & RowStatus.CHANGENODETO) == RowStatus.CHANGENODETO ||
							(status & RowStatus.DELETE) == RowStatus.DELETE ||
							(status & RowStatus.NEW) == RowStatus.NEW ||
							(status & RowStatus.NULL) == RowStatus.NULL)
						{
							menuItem_ConfirmStatus.Enabled = false;
						}
					}

					//更改页码
					if (iSelectVisibleRowCount > 1)
						menuItem_ChangePageNumber.Enabled = false;
					else
					{
						string spool_oid = dataGridView_SpoolInfo.SelectedRows[0].Cells[SpoolInfoTable.Spool_oid].Value.ToString();
						using (SMgntEntities entities = new SMgntEntities())
						{
							var result = entities.SMgnt_SpoolInfo.Where(i => i.Spool_oid == spool_oid);
							if (result.Count() == 0)
								menuItem_ChangePageNumber.Enabled = false;
						}

					}

				}
			}
		}

		/// <summary>
		/// 选择装配后，读取所选装配下的所有Spool，获取相关分区分段信息
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionSelectAssembly(object sender, EventArgs e)
		{
			comboBox_Area.Focus();
			HierarchyComboBox.HierarchyComboBoxItem oComboBoxItem = comboBox_SelectAss.SelectedItem as HierarchyComboBox.HierarchyComboBoxItem;
			if (oComboBoxItem != null)
			{
				progressBar.Maximum = 100;
				progressBar.Value = 0;

				//获取所有Spool
				List<BusinessObject> oSpoolsFromModel = CommonFunctions.GetAllSpoolFromAssmNode(oComboBoxItem.BusinessObject);
				if (oSpoolsFromModel.Count == 0)
				{
					MessageBox.Show("所选材料节点下没有小票存在，请选择其他材料节点。");
					progressBar.Value = 100;
					return;
				}

				progressBar.Value = 40;

				//获取分区分段表
				List<BusinessObject> oSpools = new List<BusinessObject>() { oSpoolsFromModel[0] };
				GetAreaBlockAndParentAssMap(oSpools);

				//设置当前选择
				string sMajor = "";
				string sArea = "";
				string sBlock = "";
				ParentAss parentAss = new ParentAss();
				CommonFunctions.GetAreaBloclAndParentAss(oSpoolsFromModel[0], ref sMajor, ref sArea, ref sBlock, ref parentAss);
				
				progressBar.Value = 80;

				//设置专业，分区、分段
				comboBox_ParentAss.SelectedIndexChanged -= ActionChangeParentAss;
				if (!comboBox_Major.Items.Contains(sMajor))
					comboBox_Major.Items.Add(sMajor);
				comboBox_Area.SelectedItem = sMajor;
				if (!comboBox_Area.Items.Contains(sArea))
					comboBox_Area.Items.Add(sArea);
				comboBox_Area.SelectedItem = sArea;
				if (!comboBox_Block.Items.Contains(sBlock))
					comboBox_Block.Items.Add(sBlock);
				comboBox_Block.SelectedItem = sBlock;
				if (!comboBox_ParentAss.Items.Contains(parentAss.Name))
					comboBox_ParentAss.Items.Add(parentAss.Name);
				comboBox_ParentAss.SelectedItem = parentAss.Name;
				comboBox_ParentAss.SelectedIndexChanged += ActionChangeParentAss;

				//获取节点下小票信息
				ActionCompareModelAndDB(null, null);

				progressBar.Value = 100;
			}
		}

		/// <summary>
		/// 从数据中获取分区和分段信息
		/// </summary>
		/// <param name="oSMgntSpools"></param>
		private void GetAreaBlockAndParentAssMap(List<SMgnt_SpoolInfo> oSMgntSpools)
		{
			Dictionary<string, Dictionary<string, List<ParentAss>>> areaAndBlockMap;
			Dictionary<string, List<ParentAss>> blockAndParentAssMap;
			List<ParentAss> parentAssList;
			foreach (var oSMgntSpool in oSMgntSpools)
			{
				if (oSMgntSpool.Zone == null)//可能存在数据提取错误的小票
					continue;

				if (m_MajorAndAreaMap.ContainsKey(oSMgntSpool.Major))
				{
					areaAndBlockMap = m_MajorAndAreaMap[oSMgntSpool.Major];
				}
				else
				{
					areaAndBlockMap = new Dictionary<string, Dictionary<string, List<ParentAss>>>();
					m_MajorAndAreaMap.Add(oSMgntSpool.Major, areaAndBlockMap);
				}

				if (areaAndBlockMap.ContainsKey(oSMgntSpool.Zone))
				{
					blockAndParentAssMap = areaAndBlockMap[oSMgntSpool.Zone];
				}
				else
				{
					blockAndParentAssMap = new Dictionary<string, List<ParentAss>>();
					areaAndBlockMap.Add(oSMgntSpool.Zone, blockAndParentAssMap);
				}

				if (blockAndParentAssMap.ContainsKey(oSMgntSpool.Block))
				{
					parentAssList = blockAndParentAssMap[oSMgntSpool.Block];
				}
				else
				{
					parentAssList = new List<ParentAss>();
					blockAndParentAssMap.Add(oSMgntSpool.Block, parentAssList);
				}

				ParentAss currParentAss = new ParentAss() { Name = oSMgntSpool.ParentAss, OID = oSMgntSpool.ParentAss_oid };
				if (!parentAssList.Contains(currParentAss))
				{
					parentAssList.Add(currParentAss);
				}
			}
		}
		private void GetAreaBlockAndParentAssMap(List<BusinessObject> oSpools)
		{
			string sMajor = "";
			string sArea = "";
			string sBlock = "";
			ParentAss parentAss = new ParentAss();
			Dictionary<string, Dictionary<string, List<ParentAss>>> areaAndBlockMap;
			Dictionary<string, List<ParentAss>> blockAndParentAssMap;
			List<ParentAss> parentAssList;
			foreach (var oSpool in oSpools)
			{
				CommonFunctions.GetAreaBloclAndParentAss(oSpool, ref sMajor, ref sArea, ref sBlock, ref parentAss);

				if (m_MajorAndAreaMap.ContainsKey(sMajor))
				{
					areaAndBlockMap = m_MajorAndAreaMap[sMajor];
				}
				else
				{
					areaAndBlockMap = new Dictionary<string, Dictionary<string, List<ParentAss>>>();
					m_MajorAndAreaMap.Add(sMajor, areaAndBlockMap);
				}

				if (areaAndBlockMap.ContainsKey(sArea))
				{
					blockAndParentAssMap = areaAndBlockMap[sArea];
				}
				else
				{
					blockAndParentAssMap = new Dictionary<string, List<ParentAss>>();
					areaAndBlockMap.Add(sArea, blockAndParentAssMap);
				}

				if (blockAndParentAssMap.ContainsKey(sBlock))
				{
					parentAssList = blockAndParentAssMap[sBlock];
				}
				else
				{
					parentAssList = new List<ParentAss>();
					blockAndParentAssMap.Add(sBlock, parentAssList);
				}

				if (!parentAssList.Contains(parentAss))
				{
					parentAssList.Add(parentAss);
				}
			}
		}

		/// <summary>
		/// 设置区域ComboBox内容
		/// </summary>
		private void SetMajor()
		{
			comboBox_Major.Items.Clear();
			comboBox_Area.Items.Clear();
			comboBox_Block.Items.Clear();
			comboBox_ParentAss.Items.Clear();

			//设置到Combo
			comboBox_Major.Items.AddRange(m_MajorAndAreaMap.Keys.ToArray());

			//默认选择第一个
			if (comboBox_Major.Items.Count != 0)
				comboBox_Major.SelectedIndex = 0;
		}

		//变更专业
		private void ActionChangeMajor(object sender, EventArgs e)
		{
			comboBox_Area.Items.Clear();
			comboBox_Block.Items.Clear();
			comboBox_ParentAss.Items.Clear();
			comboBox_Area.Text = "";
			comboBox_Block.Text = "";
			comboBox_ParentAss.Text = "";

			this.dataGridView_Assembly.DataSource = null;
			this.dataGridView_PipeBend.DataSource = null;
			this.dataGridView_PipeCheck.DataSource = null;
			this.dataGridView_PipeIssue.DataSource = null;
			this.dataGridView_PipePart.DataSource = null;
			this.dataGridView_PipeWeld.DataSource = null;
			this.dataGridView_SpoolInfo.DataSource = null;

			if (comboBox_Major.SelectedItem == null)
				return;

			Dictionary<string, Dictionary<string, List<ParentAss>>> areaAndBlockMap = m_MajorAndAreaMap[comboBox_Major.SelectedItem.ToString()];
			comboBox_Area.Items.AddRange(areaAndBlockMap.Keys.ToArray());

			if (comboBox_Area.DroppedDown == true && areaAndBlockMap.Keys.Count != 0)
				comboBox_Area.DroppedDown = false;
			comboBox_Area.DroppedDown = true;
			comboBox_Area.Focus();
		}

		/// <summary>
		/// 区域选择改变
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionChangeArea(object sender, EventArgs e)
		{
			comboBox_Block.Items.Clear();
			comboBox_ParentAss.Items.Clear();
			comboBox_Block.Text = "";
			comboBox_ParentAss.Text = "";

			this.dataGridView_Assembly.DataSource = null;
			this.dataGridView_PipeBend.DataSource = null;
			this.dataGridView_PipeCheck.DataSource = null;
			this.dataGridView_PipeIssue.DataSource = null;
			this.dataGridView_PipePart.DataSource = null;
			this.dataGridView_PipeWeld.DataSource = null;
			this.dataGridView_SpoolInfo.DataSource = null;

			if (comboBox_Area.SelectedItem == null)
				return;

			Dictionary<string, Dictionary<string, List<ParentAss>>> areaAndBlockMap = m_MajorAndAreaMap[comboBox_Major.SelectedItem.ToString()];
			Dictionary<string, List<ParentAss>> blockAndMatMap = areaAndBlockMap[comboBox_Area.SelectedItem.ToString()];
			comboBox_Block.Items.AddRange(blockAndMatMap.Keys.ToArray());

			if (comboBox_Block.DroppedDown == true && blockAndMatMap.Keys.Count != 0)
				comboBox_Block.DroppedDown = false;
			comboBox_Block.DroppedDown = true;
			comboBox_Block.Focus();
		}

		/// <summary>
		/// 分段选择改变
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionChangeBlock(object sender, EventArgs e)
		{
			comboBox_ParentAss.Items.Clear();
			comboBox_ParentAss.Text = "";

			this.dataGridView_Assembly.DataSource = null;
			this.dataGridView_PipeBend.DataSource = null;
			this.dataGridView_PipeCheck.DataSource = null;
			this.dataGridView_PipeIssue.DataSource = null;
			this.dataGridView_PipePart.DataSource = null;
			this.dataGridView_PipeWeld.DataSource = null;
			this.dataGridView_SpoolInfo.DataSource = null;

			Dictionary<string, Dictionary<string, List<ParentAss>>> areaAndBlockMap = m_MajorAndAreaMap[comboBox_Major.SelectedItem.ToString()];
			Dictionary<string, List<ParentAss>> blockAndMatMap = areaAndBlockMap[comboBox_Area.SelectedItem.ToString()];
			List<ParentAss> parentAssList = blockAndMatMap[comboBox_Block.SelectedItem.ToString()];

			foreach (var parentAss in parentAssList)
			{
				comboBox_ParentAss.Items.Add(parentAss.Name);
			}

			if (comboBox_ParentAss.DroppedDown == true && parentAssList.Count != 0)
				comboBox_ParentAss.DroppedDown = false;
			comboBox_ParentAss.DroppedDown = true;
			comboBox_ParentAss.Focus();
		}

		/// <summary>
		/// 获取当前选择小票所在的装配父节点
		/// </summary>
		/// <returns></returns>
		private ParentAss GetCurrentSelectSpoolParentAss()
		{
			//获取当前选择的分区和分段
			string sMajor = comboBox_Major.SelectedItem.ToString();
			string sArea = comboBox_Area.SelectedItem.ToString();
			string sBlock = comboBox_Block.SelectedItem.ToString();
			string sParentAss = comboBox_ParentAss.SelectedItem.ToString();

			Dictionary<string, Dictionary<string, List<ParentAss>>> areaAndBlockMap = m_MajorAndAreaMap[sMajor];
			Dictionary<string, List<ParentAss>> blockAndParentAssMap = areaAndBlockMap[sArea];
			List<ParentAss> parentAssList = blockAndParentAssMap[sBlock];

			//获取分段Bo对象
			foreach (var parrentAss in parentAssList)
			{
				if (sParentAss == parrentAss.Name)
					return parrentAss;
			}

			return null;
		}

		private void checkBox_HideNoProblemItem_Reset()
		{
			checkBox_HideNoProblemItem.Enabled = true;
			checkBox_HideNoProblemItem.CheckStateChanged -= ActionShowOrHideSpools;
			checkBox_HideNoProblemItem.CheckState = CheckState.Unchecked;
			checkBox_HideNoProblemItem.CheckStateChanged += ActionShowOrHideSpools;
		}

		/// <summary>
		/// 切换小票所在父节点
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionChangeParentAss(object sender, EventArgs e)
		{
			checkBox_HideNoProblemItem_Reset();
			textBox_Filter.Text = "";
			progressBar.Maximum = 100;
			progressBar.Value = 0;

			string sMajor = comboBox_Major.SelectedItem.ToString();
			string sArea = comboBox_Area.SelectedItem.ToString();
			string sBlock = comboBox_Block.SelectedItem.ToString();
			string sParentAss = comboBox_ParentAss.SelectedItem.ToString();
			if (sParentAss == "")
				return;
			dataGridView_SpoolInfo.Focus();
			ParentAss currParentAss = GetCurrentSelectSpoolParentAss();

			progressBar.Value = 10;

			//获取当前分段分区下数据库数据显示在界面中
			//若所选分段分区下没有数据库数据，获取模型数据加载在界面中
			//获取数据库中相关分区与分段下的小票
			using (SMgntEntities dbContext = new SMgntEntities())
			{
				List<SMgnt_SpoolInfo> listSMgntSpoolFromDB = dbContext.SMgnt_SpoolInfo.Where(i => i.Major == sMajor && i.Zone == sArea && i.Block == sBlock && i.ParentAss == sParentAss).ToList();

				if (listSMgntSpoolFromDB.Count != 0)
				{
					//从已存在的数据中获取图号
					GetDrawNo(listSMgntSpoolFromDB);

					progressBar.Value = 20;

					//初始小票状态
					foreach(var oSMgntSpool in listSMgntSpoolFromDB)
					{
						if (oSMgntSpool.IsValid == 0)
							oSMgntSpool.RowStatus = (int)RowStatus.NULL;
						else
							oSMgntSpool.RowStatus = (int)RowStatus.DBStatus;
					}

					progressBar.Value = 40;

					//构建DataTable
					DataTable currentBindingTable = SpoolInfoTable.BuildDataTable(listSMgntSpoolFromDB);

					////构建bo对象
					//foreach (DataRow row in currentBindingTable.Rows)
					//{
					//	string sName = row[SpoolInfoTable.NameDisplay].ToString();
					//	InfoLable.Text = $"构建小票{sName}的BO对象";
					//	BusinessObject oSpool = CommonFunctions.GetBusinessObjectFromOid(row[SpoolInfoTable.Spool_oid].ToString());
					//	row[SpoolInfoTable.SpoolFromModel] = oSpool;
					//}

					progressBar.Value = 60;

					//显示小票列表
					SpoolInfoTable.SetDTAndFormat(this.dataGridView_SpoolInfo, currentBindingTable);

					progressBar.Value = 90;

					//刷新小票行颜色
					UpdateSpoolDTRowColor();

					InfoLable.Text = $"数据库小票数量：{listSMgntSpoolFromDB.Count()}";
				}
				else
				{
					MessageBox.Show("当前节点在数据库中没有任何信息，即将从模型中获取小票信息。");

					ActionCompareModelAndDB(null, null);
				}
			}

			//如果当前加载行仅一行，不选择任何行
			if (dataGridView_SpoolInfo.RowCount == 1)
				dataGridView_SpoolInfo.ClearSelection();

			SetStatusLabel();

			progressBar.Value = 100;
		}

		/// <summary>
		/// 检查数据库小票状态，识别小票是否删除
		/// </summary>
		/// <param name="listSMgntSpoolFromDB"></param>
		private void CheckDBSpoolStatus(ref List<SMgnt_SpoolInfo> listSMgntSpoolFromDB)
		{
			foreach(var oSMgntSpoolFromDB in listSMgntSpoolFromDB)
			{
				InfoLable.Text = $"检查小票{oSMgntSpoolFromDB.Name}状态...";

				//校验小票所在节点是否和当前信息一致
				BusinessObject oBO = CommonFunctions.GetBusinessObjectFromOid(oSMgntSpoolFromDB.Spool_oid);
				if (oBO != null)
				{
					oSMgntSpoolFromDB.SpoolFromModel = oBO;

					//默认状态
					oSMgntSpoolFromDB.RowStatus = (int)RowStatus.UNCHANGE;
					oSMgntSpoolFromDB.SpoolStatus = "";

					//检查模型数据和数据库数据间的差别
					SMgnt_SpoolInfo oSMgntSpoolFromModel = CommonFunctions.BuildSMgntSpool(oBO, ref m_OidAndSMgntSpoolMap);
					if (oSMgntSpoolFromModel == null)
					{
						oSMgntSpoolFromDB.SpoolStatus = "计算出错";
						oSMgntSpoolFromDB.RowStatus = (int)RowStatus.ERROR;
					}
					else
					{
						if (!CommonFunctions.IsSMgntSpoolSameName(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
						{
							oSMgntSpoolFromDB.SpoolStatus += "名称修改;";
							oSMgntSpoolFromDB.RowStatus = oSMgntSpoolFromModel.RowStatus | (int)RowStatus.CHANGEATTR;
						}
						if (!CommonFunctions.IsSMgntSpoolSameAttr(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
						{
							oSMgntSpoolFromDB.SpoolStatus += "属性修改;";
							oSMgntSpoolFromDB.RowStatus = oSMgntSpoolFromModel.RowStatus | (int)RowStatus.CHANGEATTR;
						}
						if (!CommonFunctions.IsSMgntSpoolSameShape(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
						{
							oSMgntSpoolFromDB.SpoolStatus += "形状修改;";
							oSMgntSpoolFromDB.RowStatus = oSMgntSpoolFromModel.RowStatus | (int)RowStatus.CHANGEATTR;
						}
						if (!CommonFunctions.IsSMgntSpoolSameNode(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
						{
							oSMgntSpoolFromDB.SpoolStatus += "节点修改;";
							oSMgntSpoolFromDB.RowStatus = oSMgntSpoolFromModel.RowStatus | (int)RowStatus.CHANGENODETO;
						}
					}
				}
				else
				{
					if (oSMgntSpoolFromDB.IsValid == 0)
						oSMgntSpoolFromDB.RowStatus = (int)RowStatus.NULL;
					else
					{
						oSMgntSpoolFromDB.RowStatus = (int)RowStatus.DELETE;
						oSMgntSpoolFromDB.SpoolStatus = "模型已删除;";
					}
				}
			}
		}

		/// <summary>
		/// 从已存在的数据中获取图号，存在全局变量m_sDrawNo中
		/// </summary>
		/// <param name="oSMgntSpoolsFromDB"></param>
		private void GetDrawNo(List<SMgnt_SpoolInfo> oSMgntSpoolsFromDB)
		{
			m_sDrawNo = "";
			foreach (var it in oSMgntSpoolsFromDB)
			{
				if (it.DrawNo != "")
					m_sDrawNo = it.DrawNo;
			}
		}

		private FilterColor GetRowColor(DataGridViewRow row)
		{
			FilterColor filterColor = FilterColor.Undefine;

			RowStatus spoolStatus = (RowStatus)row.Cells[SpoolInfoTable.Status].Value;
			if (spoolStatus == RowStatus.DBStatus)
				return filterColor;

			if ((spoolStatus & RowStatus.ERROR) == RowStatus.ERROR)//包含错误信息
			{
				filterColor = FilterColor.Red;
			}
			else if ((spoolStatus & RowStatus.NULL) == RowStatus.NULL)//留白
			{
				filterColor = FilterColor.LightBlue;
			}
			else if ((spoolStatus & RowStatus.DELETE) == RowStatus.DELETE)//待删除
			{
				filterColor = FilterColor.Blue;
			}
			else if ((spoolStatus & RowStatus.NEW) == RowStatus.NEW)//待更新新建
			{
				filterColor = FilterColor.AntiqueWhite;
			}
			else if ((spoolStatus & RowStatus.CHANGEATTR) == RowStatus.CHANGEATTR ||//属性，节点变更
				(spoolStatus & RowStatus.CHANGENODEFROM) == RowStatus.CHANGENODEFROM ||
				(spoolStatus & RowStatus.CHANGENODETO) == RowStatus.CHANGENODETO)
			{
				filterColor = FilterColor.Cyan;
			}
			else//没有问题的按小票问题级别显示颜色
			{
				string sSpoolOid = (string)row.Cells[SpoolInfoTable.Spool_oid].Value;

				//查找issue清单,获取最高级别问题
				int iLevel = 0;
				int iTempLevel = 0;
				using (SMgntEntities mdbContext = new SMgntEntities())
				{
					var result = from u in mdbContext.SMgnt_SpoolInfo where u.Spool_oid == sSpoolOid select u;
					if (result.Count() != 0)
					{
						SMgnt_SpoolInfo oSmgntSpool = result.First();
						if (oSmgntSpool != null && oSmgntSpool.SMgnt_PipeIssue != null)
						{
							foreach (var it in oSmgntSpool.SMgnt_PipeIssue)
							{
								iTempLevel = Convert.ToInt32(it.Level);
								if (iTempLevel > iLevel)
									iLevel = iTempLevel;
							}
						}
					}
				}

				//根据问题级别设置Row颜色
				if (iLevel == 0)
				{
					filterColor = FilterColor.Green;
				}
				else if (iLevel == 1)
				{
					filterColor = FilterColor.Yellow;
				}
				else if (iLevel == 2)
				{
					filterColor = FilterColor.Orange;
				}
			}

			return filterColor;
		}

		/// <summary>
		/// 刷新小票表格颜色
		/// </summary>
		private void UpdateSpoolDTRowColor(bool refreshSelected = false)
		{
			IList rows;
			if (refreshSelected)
				rows = this.dataGridView_SpoolInfo.SelectedRows;
			else
				rows = this.dataGridView_SpoolInfo.Rows;

			bool hasIssueSpool = false;
			foreach (DataGridViewRow row in rows)
			{
				if (row.Visible == false)
					continue;

				RowStatus status = (RowStatus)row.Cells[SpoolInfoTable.Status].Value;
				if (status == RowStatus.DBStatus)
					continue;

				string sName = row.Cells[SpoolInfoTable.NameDisplay].Value.ToString();
				InfoLable.Text = $"刷新小票{sName}颜色...";

				FilterColor filterColor = GetRowColor(row);

				Color color = Color.White;
				if (filterColor == FilterColor.AntiqueWhite)
					color = Color.AntiqueWhite;
				else if (filterColor == FilterColor.Blue)
					color = Color.Blue;
				else if (filterColor == FilterColor.Cyan)
					color = Color.Cyan;
				else if (filterColor == FilterColor.Green)
					color = Color.Green;
				else if (filterColor == FilterColor.LightBlue)
					color = Color.LightBlue;
				else if (filterColor == FilterColor.Orange)
					color = Color.Orange;
				else if (filterColor == FilterColor.Red)
					color = Color.Red;
				else if (filterColor == FilterColor.Yellow)
					color = Color.Yellow;

				if (this.dataGridView_SpoolInfo.Rows[row.Index].Cells[SpoolInfoTable.NameDisplay].Style.BackColor != color)
					this.dataGridView_SpoolInfo.Rows[row.Index].Cells[SpoolInfoTable.NameDisplay].Style.BackColor = color;

				this.Update();

				InfoLable.Text = $"刷新颜色完成...";

				if (!hasIssueSpool && color != Color.Green)
					hasIssueSpool = true;
			}

			//根据颜色设置“仅显示问题小票”复选框状态
			if (checkBox_HideNoProblemItem.CheckState == CheckState.Unchecked)//非勾选状态下，判断单元格颜色来确定CheckBox状态
			{
				if (hasIssueSpool)
					checkBox_HideNoProblemItem.Enabled = true;
				else
					checkBox_HideNoProblemItem.Enabled = false;
			}
		}

		/// <summary>
		/// 小票表格选择变更，更新其他表格显示或多行选择时，锁定其他表格
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionChangeSelectSpool(object sender, EventArgs e)
		{
			//重置DataGridView数据
			this.dataGridView_PipeBend.DataSource = null;
			this.dataGridView_PipePart.DataSource = null;
			this.dataGridView_PipeWeld.DataSource = null;
			this.dataGridView_PipeCheck.DataSource = null;
			this.dataGridView_Assembly.DataSource = null;
			this.dataGridView_PipeIssue.DataSource = null;
			this.dataGridView_CDSP.DataSource = null;

			if (this.dataGridView_SpoolInfo.SelectedRows.Count > 1)
			{
				tabControl.Enabled = false;
			}
			else
			{
				tabControl.Enabled = true;

				foreach (DataGridViewRow item in this.dataGridView_SpoolInfo.SelectedRows)//仅包含一行
				{
					using (SMgntEntities mdbContext = new SMgntEntities())
					{
						//优先显示模型数据
						//从缓存中查找
						SMgnt_SpoolInfo currSMgntSpool = null;
						string sSpoolOid = (string)item.Cells[SpoolInfoTable.Spool_oid].Value;
						if (m_OidAndSMgntSpoolMap.ContainsKey(sSpoolOid))
							currSMgntSpool = m_OidAndSMgntSpoolMap[sSpoolOid];

						//从当前模型构建
						if (currSMgntSpool == null)
						{
							BusinessObject oSpool = item.Cells[SpoolInfoTable.SpoolFromModel].Value as BusinessObject;
							if (oSpool == null)
								oSpool = CommonFunctions.GetBusinessObjectFromOid(sSpoolOid);

							if (oSpool != null)
							{
								currSMgntSpool = CommonFunctions.BuildSMgntSpool(oSpool, ref m_OidAndSMgntSpoolMap, true);
							}
						}

						//从数据库查找小票
						if (currSMgntSpool == null)
						{

								var result = from u in mdbContext.SMgnt_SpoolInfo where u.Spool_oid == sSpoolOid select u;
								if (result.Count() != 0)
								{
									currSMgntSpool = result.First();
								}

						}

						if (currSMgntSpool == null)
							return;

						//显示数据到界面
						//CSSP
						{
							DataTable sourceDT = SpoolPlusInfoTable.BuildDataTable(currSMgntSpool);
							SpoolPlusInfoTable.SetDTAndFormat(this.dataGridView_CDSP, sourceDT);
						}

						//PipePart
						if (currSMgntSpool.SMgnt_PipePart != null)
						{
							//构建DataTable
							DataTable sourceDT = PipePartTable.BuildDataTable(currSMgntSpool.SMgnt_PipePart.ToList());
							PipePartTable.SetDTAndFormat(this.dataGridView_PipePart, sourceDT);
						}

						//PipeWeld
						if (currSMgntSpool.SMgnt_PipeWeld != null)
						{
							//构建DataTable
							DataTable sourceDT = PipeWeldTable.BuildDataTable(currSMgntSpool.SMgnt_PipeWeld.ToList());
							PipeWeldTable.SetDTAndFormat(this.dataGridView_PipeWeld, sourceDT);
						}

						//PipeBend
						if (currSMgntSpool.SMgnt_PipeBend != null)
						{
							//构建DataTable
							DataTable sourceDT = PipeBendTable.BuildDataTable(currSMgntSpool.SMgnt_PipeBend.ToList());
							PipeBendTable.SetDTAndFormat(this.dataGridView_PipeBend, sourceDT);
						}

						//PipeCheck
						if (currSMgntSpool.SMgnt_PipeCheck != null)
						{
							//构建DataTable
							DataTable sourceDT = PipeCheckTable.BuildDataTable(currSMgntSpool.SMgnt_PipeCheck.ToList());
							PipeCheckTable.SetDTAndFormat(this.dataGridView_PipeCheck, sourceDT);
						}

						//PipeAssembly
						if (currSMgntSpool.SMgnt_PipeAssembly != null)
						{
							//构建DataTable
							DataTable sourceDT = PipeAssemblyTable.BuildDataTable(currSMgntSpool.SMgnt_PipeAssembly.ToList());
							PipeAssemblyTable.SetDTAndFormat(this.dataGridView_Assembly, sourceDT);
						}

						//PipeIssue
						if (currSMgntSpool.SMgnt_PipeIssue != null)
						{
							//构建DataTable
							DataTable sourceDT = PipeIssueTable.BuildDataTable(currSMgntSpool.SMgnt_PipeIssue.ToList());
							PipeIssueTable.SetDTAndFormat(this.dataGridView_PipeIssue, sourceDT);

							
						}

						//默认显示Part页
						tabControl.SelectedIndex = 1;
						tabControl.SelectedIndex = 2;
						tabControl.SelectedIndex = 3;
						tabControl.SelectedIndex = 5;
						tabControl.SelectedIndex = 4;
						//有问题时，显示问题页
						if (dataGridView_PipeIssue.RowCount != 0)
							tabControl.SelectedIndex = 0;
					}
					this.dataGridView_SpoolInfo.Focus();

				}
			}

			SetStatusLabel();
		}

		/// <summary>
		/// 右键更新
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionUpdate(object sender, EventArgs e)
		{
			TransactionManager TransMgr = new TransactionManager();

			progressBar.Value = 0;
			progressBar.Maximum = this.dataGridView_SpoolInfo.SelectedRows.Count + 10;

			//当前条目存在4中状态：
			//	1、数据库不存在，模型存在 new
			//	2、数据库存在，模型不存在 delete，更新不处理这种情况，需要在删除中处理
			//	3、数据库存在，模型存在 UNCHANGE、CHANGEATTR、CHANGESHAPE
			//	4、对应模型数据存在问题 ERROR，入库基本信息
			int iRow = 0;
			foreach (DataGridViewRow row in this.dataGridView_SpoolInfo.SelectedRows)
			{
				iRow++;
				progressBar.Value = iRow;

				//跳过非显示项
				if (row.Visible == false)
					continue;

				using (SMgntEntities mdbContext = new SMgntEntities())
				{
					RowStatus status = (RowStatus)row.Cells[SpoolInfoTable.Status].Value;
					if (status == RowStatus.UNCHANGE ||
						(status & RowStatus.DELETE) == RowStatus.DELETE ||
						(status & RowStatus.NULL) == RowStatus.NULL ||
						status == RowStatus.ERROR)//跳过没有变化的行，删除行，空白行，ERROR行
						continue;

					string sSpoolOid = (string)row.Cells[SpoolInfoTable.Spool_oid].Value;
					string sSpoolName = row.Cells[SpoolInfoTable.NameDisplay].Value.ToString();
					string sSpoolStatus = row.Cells[SpoolInfoTable.SpoolStatusDisplay].Value.ToString();

					////检查权限
					//if (row.Cells[SpoolInfoTable.ResponseDisplay].Value.ToString() != m_sCurrentUser)
					//{
					//	InfoLable.Text = $"当前用户无权限修改小票{sSpoolName}，跳过此小票...";
					//	continue;
					//}

					//查找模型Spool对象，构建SMgnt信息
					BusinessObject oSpool = row.Cells[SpoolInfoTable.SpoolFromModel].Value as BusinessObject;
					if (oSpool == null)
					{
						oSpool = CommonFunctions.GetBusinessObjectFromOid(sSpoolOid);
						if (oSpool == null)
						{
							MessageBox.Show($"小票{row.Cells[SpoolInfoTable.SpoolFullName].Value}对应的模型找不到。");
							continue;
						}
					}
					SMgnt_SpoolInfo oSMgntSpoolFromModel = CommonFunctions.BuildSMgntSpool(oSpool, ref m_OidAndSMgntSpoolMap);
					if (oSMgntSpoolFromModel == null)
					{
						InfoLable.Text = $"小票{row.Cells[SpoolInfoTable.SpoolFullName].Value}对应的模型计算时出错，无法更新。";
						continue;
					}

					//检查小票是否为空小票或问题小票（检查必要属性）
					if (oSMgntSpoolFromModel.Spool_oid == "" ||
						oSMgntSpoolFromModel.CalculateCode == "" ||
						oSMgntSpoolFromModel.Spool == "" || 
						oSMgntSpoolFromModel.Major == "" ||
						oSMgntSpoolFromModel.Zone == "" || 
						oSMgntSpoolFromModel.Block == "" || 
						oSMgntSpoolFromModel.Block_oid == "" ||
						oSMgntSpoolFromModel.ParentAss == "" ||
						oSMgntSpoolFromModel.ParentAss_oid == "" ||
						oSMgntSpoolFromModel.Name == "")
					{
						MessageBox.Show($"小票{row.Cells[SpoolInfoTable.SpoolFullName].Value}对应的模型计算时出错，无法更新（可能为空小票），请检查该小票是否正常后再更新。");
						continue;
					}


					//更新前，检查管部件和焊点是否在其他小票存在，若存在，则从数据库删除相应的管部件及焊点，并修改相应小票物理码（以便标识修改）
					CheckPartAndWeld(mdbContext, oSMgntSpoolFromModel);

					//保留小票状态
					oSMgntSpoolFromModel.SpoolStatus = sSpoolStatus;
					oSMgntSpoolFromModel.CreateBy = m_sCurrentUser;

					//刷新小票数据
					if (status == RowStatus.CHANGEOID)
					{
						//删除原小票，添加当前小票
						var result = from u in mdbContext.SMgnt_SpoolInfo where u.Spool_oid == sSpoolOid select u;
						if (result.Count() != 0)//在数据库中有对应的小票存在
						{
							SMgnt_SpoolInfo oSMgntSpoolFromDB = result.First();

							oSMgntSpoolFromModel.DrawNo = oSMgntSpoolFromDB.DrawNo;
							oSMgntSpoolFromModel.PageNo = oSMgntSpoolFromDB.PageNo;

							mdbContext.SMgnt_SpoolInfo.Remove(oSMgntSpoolFromDB);

							mdbContext.SMgnt_SpoolInfo.Add(oSMgntSpoolFromModel);

							row.Cells[SpoolInfoTable.Status].Value = status & (~RowStatus.CHANGEOID) | RowStatus.UPDATE;

							row.Cells[SpoolInfoTable.Spool_oid].Value = oSMgntSpoolFromModel.Spool_oid;
						}
					}
					if (status == RowStatus.ERROR ||
						status == RowStatus.UNCHANGE ||
						(status & RowStatus.CHANGEATTR) == RowStatus.CHANGEATTR ||
						(status & RowStatus.CHANGENODEFROM) == RowStatus.CHANGENODEFROM ||
						(status & RowStatus.CHANGENODETO) == RowStatus.CHANGENODETO ||
						status == RowStatus.DBStatus)
					{
						//更新
						var result = from u in mdbContext.SMgnt_SpoolInfo where u.Spool_oid == sSpoolOid select u;
						if (result.Count() != 0)//在数据库中有对应的小票存在
						{
							SMgnt_SpoolInfo oSMgntSpoolFromDB = result.First();

							//从模型复制小票数据
							CommonFunctions.CopyAttr(ref oSMgntSpoolFromDB, oSMgntSpoolFromModel);

							//针对修改走的小票，将图号和页码重置
							if ((status & RowStatus.CHANGENODETO) == RowStatus.CHANGENODETO)
							{
								oSMgntSpoolFromDB.DrawNo = "";
								oSMgntSpoolFromDB.PageNo = "";
							}

							//修改小票状态为已更新
							oSMgntSpoolFromDB.SpoolStatus = "已更新";

							//删除数据库旧数据
							mdbContext.SMgnt_PipePart.RemoveRange(oSMgntSpoolFromDB.SMgnt_PipePart);
							mdbContext.SMgnt_PipeWeld.RemoveRange(oSMgntSpoolFromDB.SMgnt_PipeWeld);
							mdbContext.SMgnt_PipeBend.RemoveRange(oSMgntSpoolFromDB.SMgnt_PipeBend);
							mdbContext.SMgnt_PipeCheck.RemoveRange(oSMgntSpoolFromDB.SMgnt_PipeCheck);
							mdbContext.SMgnt_PipeAssembly.RemoveRange(oSMgntSpoolFromDB.SMgnt_PipeAssembly);
							mdbContext.SMgnt_PipeIssue.RemoveRange(oSMgntSpoolFromDB.SMgnt_PipeIssue);

							//添加新子件
							oSMgntSpoolFromDB.SMgnt_PipePart = oSMgntSpoolFromModel.SMgnt_PipePart;
							oSMgntSpoolFromDB.SMgnt_PipeWeld = oSMgntSpoolFromModel.SMgnt_PipeWeld;
							oSMgntSpoolFromDB.SMgnt_PipeBend = oSMgntSpoolFromModel.SMgnt_PipeBend;
							oSMgntSpoolFromDB.SMgnt_PipeCheck = oSMgntSpoolFromModel.SMgnt_PipeCheck;
							oSMgntSpoolFromDB.SMgnt_PipeAssembly = oSMgntSpoolFromModel.SMgnt_PipeAssembly;
							oSMgntSpoolFromDB.SMgnt_PipeIssue = oSMgntSpoolFromModel.SMgnt_PipeIssue;

						}

						//修改到其他节点下，状态变更为DELETE
						if ((status & RowStatus.CHANGENODETO) == RowStatus.CHANGENODETO)
						{
							row.Cells[SpoolInfoTable.Status].Value = (int)RowStatus.DELETED;
						}
						//修改为当前节点下，修改属性
						if (status == RowStatus.DBStatus ||
							(status & RowStatus.CHANGENODEFROM) == RowStatus.CHANGENODEFROM ||
							(status & RowStatus.CHANGEATTR) == RowStatus.CHANGEATTR)
						{
							row.Cells[SpoolInfoTable.Status].Value = status & (~RowStatus.DBStatus) & (~RowStatus.CHANGENODEFROM) & (~RowStatus.CHANGEATTR) | RowStatus.UPDATE;
						}
						else
						{
							row.Cells[SpoolInfoTable.Status].Value = status | RowStatus.UPDATE;
						}
					}
					else if ((status & RowStatus.NEW) == RowStatus.NEW)
					{
						oSMgntSpoolFromModel.DrawNo = m_sDrawNo;//写入已有图号
						oSMgntSpoolFromModel.SpoolStatus = "已更新";
						mdbContext.SMgnt_SpoolInfo.Add(oSMgntSpoolFromModel);

						row.Cells[SpoolInfoTable.Status].Value = status & (~RowStatus.NEW) | RowStatus.UPDATE;
					}

					try
					{
						mdbContext.SaveChanges();
						InfoLable.Text = $"小票{sSpoolName}更新成功...";
					}
					catch (Exception ex)
					{
						MessageBox.Show("更新出错，原因:" + ex.InnerException.InnerException);
						InfoLable.Text = $"小票{sSpoolName}更新失败...";
						row.Cells[SpoolInfoTable.Status].Value = status | RowStatus.UPDATE;//状态还原
					}
				}

				TransMgr.Abort();
			}

			//过滤
			FilterRows();

			progressBar.Value = progressBar.Value + 5;

			//刷新界面显示，包括移除用户已删除行
			RefreshSpoolDataGridView(true);

			progressBar.Value = progressBar.Value + 5;

			//调用下选择小票响应
			ActionChangeSelectSpool(sender, e);

			InfoLable.Text = "更新完成...";
		}

		/// <summary>
		/// 更新前，检查管部件和焊点是否在其他小票存在，若存在，则从数据库删除相应的管部件及焊点，并修改相应小票物理码（以便标识修改）
		/// </summary>
		/// <param name="dbContext"></param>
		/// <param name="oSMgntSpoolFromModel"></param>
		private void CheckPartAndWeld(SMgntEntities dbContext, SMgnt_SpoolInfo oSMgntSpoolFromModel)
		{
			string oSpoolOid = oSMgntSpoolFromModel.Spool_oid;
			foreach(var oPart in oSMgntSpoolFromModel.SMgnt_PipePart)
			{
				var result = dbContext.SMgnt_PipePart.Where(i => i.Part_oid == oPart.Part_oid && i.Spool_oid != oSpoolOid);
				if (result.Count() != 0)
				{
					//移除掉这个部件，并修改Spool的物理码
					string sTempSpoolOid = result.First().Spool_oid;
					dbContext.SMgnt_PipePart.Remove(result.First());

					dbContext.SMgnt_SpoolInfo.Where(i => i.Spool_oid == sTempSpoolOid).First().CalculateCode = "0";
					dbContext.SMgnt_SpoolInfo.Where(i => i.Spool_oid == sTempSpoolOid).First().SpoolStatus = "管部件修改";
				}
			}

			foreach (var oWeld in oSMgntSpoolFromModel.SMgnt_PipeWeld)
			{
				var result = dbContext.SMgnt_PipeWeld.Where(i => i.Weld_oid == oWeld.Weld_oid && i.Spool_oid != oSpoolOid);
				if (result.Count() != 0)
				{
					//移除掉这个部件，并修改Spool的物理码
					string sTempSpoolOid = result.First().Spool_oid;
					dbContext.SMgnt_PipeWeld.Remove(result.First());

					dbContext.SMgnt_SpoolInfo.Where(i => i.Spool_oid == sTempSpoolOid).First().CalculateCode = "0";
					dbContext.SMgnt_SpoolInfo.Where(i => i.Spool_oid == sTempSpoolOid).First().SpoolStatus = "焊点修改";
				}
			}
		}

		/// <summary>
		/// 右键删除
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionDelete(object sender, EventArgs e)
		{
			progressBar.Value = 0;
			progressBar.Maximum = this.dataGridView_SpoolInfo.SelectedRows.Count+1;

			//获取当前选择的分区和分段
			string sArea = comboBox_Area.SelectedItem.ToString();
			string sBlock = comboBox_Block.SelectedItem.ToString();

			int iRow = 0;
			List<DataGridViewRow> removeRows = new List<DataGridViewRow>();
			DataTable sourceTable = dataGridView_SpoolInfo.DataSource as DataTable;
			foreach (DataGridViewRow row in dataGridView_SpoolInfo.SelectedRows)
			{
				if (row.Visible == false)
					continue;

				string sSpoolName = row.Cells[SpoolInfoTable.NameDisplay].Value.ToString();
				string sSpoolOid = row.Cells[SpoolInfoTable.Spool_oid].Value.ToString();

				//跳过数据出现问题的小票
				RowStatus status = (RowStatus)row.Cells[SpoolInfoTable.Status].Value;
				//if (status == SpoolStatus.ERROR)
				//{
				//	InfoLable.Text = $"小票{sSpoolName}数据异常，跳过此小票...";
				//	continue;
				//}

				//检查权限
				//string sSpoolOid = (string)row.Cells[SpoolInfoTable.Spool_oid].Value;			
				//if (row.Cells[SpoolInfoTable.ResponseDisplay].Value.ToString() != m_sCurrentUser)
				//{
				//	InfoLable.Text = $"当前用户无权限修改小票{sSpoolName}，跳过此小票...";
				//	continue;
				//}

				//删除旧的DB数据（不论是小票在模型中未找到，还是有对应的小票，都先删除后更新）
				using (SMgntEntities mdbContext = new SMgntEntities())
				{
					var result = from u in mdbContext.SMgnt_SpoolInfo where u.Spool_oid == sSpoolOid select u;
					if (result.Count() != 0)
					{
						try
						{
							mdbContext.Entry(result.First()).State = System.Data.Entity.EntityState.Deleted;

							//向删除历史中添加记录
							SMgnt_History oSMgntHistory = new SMgnt_History();
							oSMgntHistory.Spool = result.First().Spool;
							oSMgntHistory.Spool_oid = result.First().Spool_oid;
							oSMgntHistory.CreateBy = result.First().CreateBy;
							oSMgntHistory.DeleteBy = m_sCurrentUser;
							oSMgntHistory.CreateTime = result.First().CreateTime;

							mdbContext.SMgnt_History.Add(oSMgntHistory);

							mdbContext.SaveChanges();
						}
						catch (Exception ex)
						{
							MessageBox.Show(ex.Message);
							continue;
						}
					}
				}

				//找到对应小票
				BusinessObject oSpool = row.Cells[SpoolInfoTable.SpoolFromModel].Value as BusinessObject;
				if (oSpool != null)
				{
					SMgnt_SpoolInfo oSMgntSpool = CommonFunctions.BuildSMgntSpool(oSpool, ref m_OidAndSMgntSpoolMap);
					if (oSMgntSpool != null)
					{
						SpoolInfoTable.SetRowContent(row, oSMgntSpool);
					}

					row.Cells[SpoolInfoTable.Status].Value = RowStatus.NEW | (RowStatus)oSMgntSpool.RowStatus;
					row.Cells[SpoolInfoTable.SpoolStatusDisplay].Value = "新建;";
				}
				else
				{
					row.Cells[SpoolInfoTable.Status].Value = RowStatus.DELETED;
				}

				InfoLable.Text = $"小票{sSpoolName}删除成功...";

				progressBar.Value = ++iRow;
			}

			//刷新界面显示
			RefreshSpoolDataGridView(true);
			progressBar.Value = progressBar.Maximum;

			InfoLable.Text = $"删除完成...";
		}

		/// <summary>
		/// 刷新界面显示，包括移除用户已删除行
		/// </summary>
		private void RefreshSpoolDataGridView(bool refreshSelected = false)
		{
			IList rows;
			if (refreshSelected)
				rows = this.dataGridView_SpoolInfo.SelectedRows;
			else
				rows = this.dataGridView_SpoolInfo.Rows;

			//更新列表
			List<DataGridViewRow> removeRows = new List<DataGridViewRow>();
			foreach (DataGridViewRow row in rows)
			{
				if (row.Visible == false)
					continue;

				string sName = row.Cells[SpoolInfoTable.NameDisplay].Value.ToString();
				InfoLable.Text = $"刷新小票{sName}内容...";

				RowStatus iStatus = (RowStatus)row.Cells[SpoolInfoTable.Status].Value;
				if (iStatus == RowStatus.DELETED)//删除行
				{
					removeRows.Add(row);
				}
				else if ((iStatus & RowStatus.UPDATE) == RowStatus.UPDATE)//更新行
				{
					//更新整行数据
					using (SMgntEntities mdbContext = new SMgntEntities())
					{
						string sSpoolOid = (string)row.Cells[SpoolInfoTable.Spool_oid].Value;
						var list = (from u in mdbContext.SMgnt_SpoolInfo where u.Spool_oid == sSpoolOid select u).ToList();
						if (list.Count() != 0)
						{
							(list[0]).SpoolFromModel = row.Cells[SpoolInfoTable.SpoolFromModel].Value;
							
							(list[0]).RowStatus = (int)iStatus & (~(int)RowStatus.UPDATE);

							SpoolInfoTable.SetRowContent(row, list[0]);
						}
					}
				}
			}
			foreach (var row in removeRows)
				dataGridView_SpoolInfo.Rows.Remove(row);
			this.dataGridView_SpoolInfo.Update();

			//更新颜色
			UpdateSpoolDTRowColor(refreshSelected);
		}

		/// <summary>
		/// 小票表格排序后，重新着色
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionSortSpool(object sender, EventArgs e)
		{
			//更新颜色
			UpdateSpoolDTRowColor();
		}

		/// <summary>
		/// 显示输入图号对话框
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionShowInputDrawNoFrom(object sender, EventArgs e)
		{
			if (this.dataGridView_SpoolInfo.Rows.Count == 0)
			{
				MessageBox.Show("请选择数据后在输入图号。");
				return;
			}

			InputForm inputForm = new InputForm();
			inputForm.SetText(m_sDrawNo);
			inputForm.OnConfirmClick += ActionChangeDrawNo;
			inputForm.ShowDialog();
		}

		/// <summary>
		/// 输入图号
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionChangeDrawNo(object sender, EventArgs e)
		{
			InputForm inputForm = sender as InputForm;
			m_sDrawNo = inputForm.GetText();
			inputForm.Close();
			if (m_sDrawNo != "")
			{
				using (SMgntEntities dbContext = new SMgntEntities())
				{
					foreach(DataGridViewRow row in this.dataGridView_SpoolInfo.Rows)
					{
						string sSpoolOid = (string)row.Cells[SpoolInfoTable.Spool_oid].Value;
						var result = from u in dbContext.SMgnt_SpoolInfo where u.Spool_oid == sSpoolOid select u;
						if (result.Count() != 0)
						{
							result.First().DrawNo = m_sDrawNo;
							dbContext.Entry(result.First()).State = System.Data.Entity.EntityState.Modified;
							dbContext.SaveChanges();

							row.Cells[SpoolInfoTable.DrawNoDisplay].Value = m_sDrawNo;
						}
					}
				}
			}
		}

		/// <summary>
		/// 插入页码，仅对已经保存到数据库的对象设置页码
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionShowInputStartPageNoForm(object sender, EventArgs e)
		{
			if (this.dataGridView_SpoolInfo.Rows.Count == 0)
			{
				MessageBox.Show("请选择数据后在插入页码。");
				return;
			}

			DialogResult dlgResult = MessageBox.Show("即将全部重排页码，是否确定执行？", "确定重排页码", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
			if (dlgResult == DialogResult.Cancel)
				return;

			InputForm inputForm = new InputForm();
			inputForm.Text = "输入起始页码";
			inputForm.SetText("1");
			inputForm.OnConfirmClick += ActionInputStartPageNo; ;
			inputForm.ShowDialog();
		}

		private void ActionInputStartPageNo(object sender, EventArgs e)
		{
			InputForm inputForm = sender as InputForm;
			string sStartPageNo = inputForm.GetText();
			if (sStartPageNo == "")
			{
				MessageBox.Show("输入了空字符，请输入非空页码。");
				inputForm.ShowDialog(this);
				return;
			}

			int iStartNo = 1;
			try
			{
				iStartNo = Convert.ToInt32(sStartPageNo);
			}
			catch
			{
				MessageBox.Show("输入格式有误，请重新输入。");
				inputForm.ShowDialog(this);
				return;
			}
			
			inputForm.Close();

			//获取当前选择的分区和分段
			string sMajor = comboBox_Major.SelectedItem.ToString();
			string sArea = comboBox_Area.SelectedItem.ToString();
			string sBlock = comboBox_Block.SelectedItem.ToString();
			string sParentAss = comboBox_ParentAss.SelectedItem.ToString();

			using (SMgntEntities dbContext = new SMgntEntities())
			{
				var result = dbContext.SMgnt_SpoolInfo.Where(i => i.Major == sMajor && i.Zone == sArea && i.Block == sBlock && i.ParentAss == sParentAss).OrderBy(i => i.Name);
				foreach (var oSMgntSpool in result)
				{
					oSMgntSpool.PageNo = iStartNo.ToString();
					dbContext.Entry(oSMgntSpool).State = System.Data.Entity.EntityState.Modified;
					iStartNo++;
				}

				dbContext.SaveChanges();

				//更新页码显示
				foreach (DataGridViewRow row in this.dataGridView_SpoolInfo.Rows)
				{
					string sSpoolOid = (string)row.Cells[SpoolInfoTable.Spool_oid].Value;

					var spools = dbContext.SMgnt_SpoolInfo.Where(i => i.Spool_oid == sSpoolOid);
					if (spools.Count() != 0)
						row.Cells[SpoolInfoTable.PageNoDisplay].Value = spools.First().PageNo;
				}

				InfoLable.Text = $"{result.Count()}个小票完成页码编排...";
			}
		}

		/// <summary>
		/// 显示页码输入框
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionShowInputPageNoForm(object sender, EventArgs e)
		{
			string sPageNo = dataGridView_SpoolInfo.SelectedRows[0].Cells[SpoolInfoTable.PageNoDisplay].Value.ToString();

			InputForm inputForm = new InputForm();
			inputForm.Text = "输入页码";
			inputForm.SetText(sPageNo);
			inputForm.OnConfirmClick += ActionChangePageNo;
			inputForm.ShowDialog();
		}

		/// <summary>
		/// 单独修改某一行页码
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionChangePageNo(object sender, EventArgs e)
		{
			InputForm inputForm = sender as InputForm;
			string sPageNo = inputForm.GetText();
			inputForm.Close();
			if (sPageNo == "")
			{
				MessageBox.Show("输入了空字符，请输入非空页码。");
				ActionShowInputPageNoForm(null, null);
				return;
			}

			//获取当前选择的分区和分段
			string sMajor = comboBox_Major.SelectedItem.ToString();
			string sArea = comboBox_Area.SelectedItem.ToString();
			string sBlock = comboBox_Block.SelectedItem.ToString();
			string sParentAss = comboBox_ParentAss.SelectedItem.ToString();

			//当前选择行对应的小票数据库id
			string sSpoolOid = (string)dataGridView_SpoolInfo.SelectedRows[0].Cells[SpoolInfoTable.Spool_oid].Value;
			using (SMgntEntities dbContext = new SMgntEntities())
			{
				//查找是否已经有其他小票使用了这个页码
				var querySamePage = dbContext.SMgnt_SpoolInfo.Where(i => i.Spool_oid != sSpoolOid && i.Major == sMajor && i.Zone == sArea && i.Block == sBlock && i.ParentAss == sParentAss && i.PageNo == sPageNo);
				if (querySamePage.Count() != 0)
				{
					MessageBox.Show($"小票{querySamePage.First().Name}已经使用了页码{sPageNo},请重新输入正确页码。");
					ActionShowInputPageNoForm(null, null);
					return;
				}

				var result = dbContext.SMgnt_SpoolInfo.Where(i => i.Spool_oid == sSpoolOid);
				foreach (var oSMgntSpool in result)
				{
					oSMgntSpool.PageNo = sPageNo;
					dbContext.Entry(oSMgntSpool).State = System.Data.Entity.EntityState.Modified;
				}

				if (result.Count() != 0)
				{
					dbContext.SaveChanges();

					//更新页码显示
					dataGridView_SpoolInfo.SelectedRows[0].Cells[SpoolInfoTable.PageNoDisplay].Value = sPageNo;
				}
			}
		}

		/// <summary>
		/// 行双击事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionDoubleClickRow(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex < 0)
				return;

			ClientServiceProvider.SelectSet.SelectedObjects.Clear();
			Spool oSpool = this.dataGridView_SpoolInfo.Rows[e.RowIndex].Cells[SpoolInfoTable.SpoolFromModel].Value as Spool;
			if (oSpool != null)
			{
				CommonFunctions.FitObject(oSpool);
			}
			else
			{
				BusinessObject oBOSpool = CommonFunctions.GetBusinessObjectFromOid(dataGridView_SpoolInfo.Rows[e.RowIndex].Cells[SpoolInfoTable.Spool_oid].Value.ToString());
				dataGridView_SpoolInfo.Rows[e.RowIndex].Cells[SpoolInfoTable.SpoolFromModel].Value = oBOSpool;

				if (oBOSpool != null)
					CommonFunctions.FitObject(oBOSpool);
			}
		}

		/// <summary>
		/// 模型与数据库数据进行对比
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionCompareModelAndDB(object sender, EventArgs e)
		{
			if (comboBox_Area.SelectedItem == null ||
				comboBox_Block.SelectedItem == null ||
				comboBox_ParentAss.SelectedItem == null)
			{
				MessageBox.Show("请选择相应节点。");
				return;
			}

			//清除缓存
			m_OidAndSMgntSpoolMap.Clear();
			checkBox_HideNoProblemItem_Reset();
			textBox_Filter.Text = "";
			progressBar.Maximum = 100;
			progressBar.Value = 0;

			string sMajor = comboBox_Major.SelectedItem.ToString();
			string sArea = comboBox_Area.SelectedItem.ToString();
			string sBlock = comboBox_Block.SelectedItem.ToString();
			string sParentAss = comboBox_ParentAss.SelectedItem.ToString();
			if (sParentAss == "")
				return;

			InfoLable.Text = "获取所选节点BO对象...";

			dataGridView_SpoolInfo.Focus();
			ParentAss currParentAss = GetCurrentSelectSpoolParentAss();

			//获取装配节点对应的BO对象
			BusinessObject oBOOnParentAss = CommonFunctions.GetBusinessObjectFromOid(currParentAss.OID);
			if (oBOOnParentAss == null)
			{
				MessageBox.Show($"装配节点{currParentAss.Name}{currParentAss.OID}找不到对应节点对象，节点可能已经删除。");
				return;
			}

			progressBar.Value = 10;
			InfoLable.Text = "获取所选节点下所有小票...";

			//获取所选节点下所有Spool
			List<BusinessObject> oSpoolsFromModel = CommonFunctions.GetAllSpoolFromAssmNode(oBOOnParentAss);

			//获取小票信息
			List<SMgnt_SpoolInfo> oSMgntSpoolsFromModel = CommonFunctions.BuildSMgntSpoolList( oSpoolsFromModel, ref m_OidAndSMgntSpoolMap, InfoLable);

			progressBar.Value = 30;

			//获取模型部分小票状态
			List<string> matchedSpoolOidListFromDB = new List<string>();
			List<SMgnt_SpoolInfo> combineSMgntSpoolList = new List<SMgnt_SpoolInfo>();
			for (int i=0; i<oSMgntSpoolsFromModel.Count; i++)
			//foreach(var oSMgntSpoolFromModel in oSMgntSpoolsFromModel)
			{
				var oSMgntSpoolFromModel = oSMgntSpoolsFromModel[i];

				InfoLable.Text = $"检查小票{oSMgntSpoolFromModel.Name}状态...";

				SMgnt_SpoolInfo oSMgntFromDB = FindSMgntSpoolFromDB(oSMgntSpoolFromModel);
				if (oSMgntFromDB == null)//未匹配
				{
					oSMgntSpoolFromModel.RowStatus = oSMgntSpoolFromModel.RowStatus | (int)RowStatus.NEW;
					oSMgntSpoolFromModel.SpoolStatus = "新建;";
					combineSMgntSpoolList.Add(oSMgntSpoolFromModel);
				}
				else//匹配
				{
					CheckRowAndSpoolStatus(ref oSMgntFromDB, ref oSMgntSpoolFromModel);
					combineSMgntSpoolList.Add(oSMgntSpoolFromModel);
					matchedSpoolOidListFromDB.Add(oSMgntFromDB.Spool_oid);
				}
			}

			progressBar.Value = 50;

			//获取剩余数据库小票状态(当前节点下小票模型以外，属于当前节点的数据库小票)
			int iCount = 0;
			using (SMgntEntities mdbContext = new SMgntEntities())
			{
				//查询
				List<SMgnt_SpoolInfo> listSMgntSpoolFromDB = (from SMgntSpool1 in mdbContext.SMgnt_SpoolInfo
															  where SMgntSpool1.Major == sMajor &&
															  SMgntSpool1.Zone == sArea && 
															  SMgntSpool1.Block == sBlock && 
															  SMgntSpool1.ParentAss == sParentAss &&
															  matchedSpoolOidListFromDB.Contains(SMgntSpool1.Spool_oid) == false
															  select SMgntSpool1).ToList();

				iCount = listSMgntSpoolFromDB.Count;

				//检查数据库小票状态
				CheckDBSpoolStatus(ref listSMgntSpoolFromDB);

				combineSMgntSpoolList.AddRange(listSMgntSpoolFromDB);

				progressBar.Value = 70;
			}

			//构建DataTable
			InfoLable.Text = $"构建显示表格...";
			DataTable currentBindingTable = SpoolInfoTable.BuildDataTable(combineSMgntSpoolList);

			progressBar.Value = 80;

			//显示小票列表
			SpoolInfoTable.SetDTAndFormat(this.dataGridView_SpoolInfo, currentBindingTable);

			progressBar.Value = 90;

			//刷新小票行颜色
			UpdateSpoolDTRowColor();

			//如果当前加载行仅一行，不选择任何行
			if (dataGridView_SpoolInfo.RowCount == 1)
				dataGridView_SpoolInfo.ClearSelection();

			SetStatusLabel();

			FilterRows();

			progressBar.Value = 100;

			InfoLable.Text = $"数据库小票数量：{matchedSpoolOidListFromDB.Count() + iCount}，模型中小票数量：{oSMgntSpoolsFromModel.Count}";
		}

		/// <summary>
		/// 从当前选择的小票模型中获取信息并加载到界面中
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionLoadCurrentSelecctModel(object sender, EventArgs e)
		{
			checkBox_HideNoProblemItem_Reset();
			textBox_Filter.Text = "";
			progressBar.Maximum = 100;
			progressBar.Value = 0;

			//获取分区，分段，节点信息
			List<BusinessObject> oBOOfSpools = GetSpoolsAndSetZoneBlockMatFromSelectSet();
			if (oBOOfSpools == null)
				return;

			progressBar.Value = 20;

			//构建模型小票信息
			List<SMgnt_SpoolInfo> oSMgntSpoolsFromModel = CommonFunctions.BuildSMgntSpoolList(oBOOfSpools, ref m_OidAndSMgntSpoolMap);

			progressBar.Value = 40;

			//匹配数据库数据，获取状态
			List<SMgnt_SpoolInfo> combineSMgntSpoolList = new List<SMgnt_SpoolInfo>();
			for(int i=0; i<oSMgntSpoolsFromModel.Count; i++)
			//foreach (var oSMgntSpoolFromModel in oSMgntSpoolsFromModel)
			{
				var oSMgntSpoolFromModel = oSMgntSpoolsFromModel[i];
				SMgnt_SpoolInfo oSMgntFromDB = FindSMgntSpoolFromDB(oSMgntSpoolFromModel);
				if (oSMgntFromDB == null)//未匹配
				{
					oSMgntSpoolFromModel.RowStatus = oSMgntSpoolFromModel.RowStatus | (int)RowStatus.NEW;
					oSMgntSpoolFromModel.SpoolStatus = "新建;";
					combineSMgntSpoolList.Add(oSMgntSpoolFromModel);
				}
				else//匹配
				{
					CheckRowAndSpoolStatus(ref oSMgntFromDB, ref oSMgntSpoolFromModel);
					combineSMgntSpoolList.Add(oSMgntSpoolFromModel);
				}
			}

			//构建DataTable
			DataTable currentBindingTable = SpoolInfoTable.BuildDataTable(combineSMgntSpoolList);

			progressBar.Value = 60;

			//显示小票列表
			SpoolInfoTable.SetDTAndFormat(this.dataGridView_SpoolInfo, currentBindingTable);

			progressBar.Value = 80;

			//刷新颜色
			UpdateSpoolDTRowColor();

			//如果当前加载行仅一行，不选择任何行
			if (dataGridView_SpoolInfo.RowCount == 1)
				dataGridView_SpoolInfo.ClearSelection();

			progressBar.Value = 100;

			SetStatusLabel();

			InfoLable.Text = $"模型小票数量：{oSMgntSpoolsFromModel.Count()}";

			//Dictionary<string, string> dicPartOIDtoTAG = new Dictionary<string, string>();
			//Dictionary<string, string> dicWeldOIDtoTAG = new Dictionary<string, string>();
			//string sSpoolsMat = "";
			//string sSpoolsWeld = "";
			//string sSpoolsAssembly = "";
			//string sSpoolsBending = "";
			//string sSpoolsChecking = "";
			//string sSpoolsStartEnd = "";
			//string sSpoolsDrawingProps = "";
			//DataQueryService.AchieveData(oBOOfSpools[0].ObjectID,
			//ref dicPartOIDtoTAG,
			//ref dicWeldOIDtoTAG,
			//ref sSpoolsMat,
			//ref sSpoolsWeld,
			//ref sSpoolsAssembly,
			//ref sSpoolsBending,
			//ref sSpoolsChecking,
			//ref sSpoolsStartEnd,
			//ref sSpoolsDrawingProps);
		}

		/// <summary>
		/// 检查模型小票状态，识别模型和数据库数据间的差别
		/// </summary>
		/// <param name="listSMgntSpoolFromModel"></param>
		private void CheckModelSpoolStatus(ref List<SMgnt_SpoolInfo> listSMgntSpoolFromModel, ref List<string> listOfMatchedSpoolOidFromDB)
		{
			//遍历小票，获取是否在数据库存在
			for (int i = 0; i < listSMgntSpoolFromModel.Count; i++)
			{
				SMgnt_SpoolInfo oSMgntSpoolFromModel = listSMgntSpoolFromModel[i];
				string sSpoolOidFromDB = "";
				CheckModelSpoolStatus(ref oSMgntSpoolFromModel, ref sSpoolOidFromDB);
				if (sSpoolOidFromDB != "")
					listOfMatchedSpoolOidFromDB.Add(sSpoolOidFromDB);
			}
		}
		private void CheckModelSpoolStatus(ref SMgnt_SpoolInfo oSMgntSpoolFromModel, ref string sSpoolOidFromDB)
		{
			SMgnt_SpoolInfo oSMgntSpoolFromDB = FindSMgntSpoolFromDB(oSMgntSpoolFromModel);
			if (oSMgntSpoolFromDB == null)
			{
				oSMgntSpoolFromModel.SpoolStatus = "新建;";
				oSMgntSpoolFromModel.RowStatus = (int)RowStatus.NEW;
			}
			else
			{
				oSMgntSpoolFromModel.RowStatus = (int)RowStatus.UNCHANGE;
				sSpoolOidFromDB = oSMgntSpoolFromDB.Spool_oid;

				//检查模型数据和数据库数据间的差别
				if (!CommonFunctions.IsSMgntSpoolSameName(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
				{
					oSMgntSpoolFromModel.SpoolStatus += "名称修改;";
					oSMgntSpoolFromModel.RowStatus = oSMgntSpoolFromModel.RowStatus | (int)RowStatus.CHANGEATTR;
				}
				if (!CommonFunctions.IsSMgntSpoolSameAttr(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
				{
					oSMgntSpoolFromModel.SpoolStatus += "属性修改;";
					oSMgntSpoolFromModel.RowStatus = oSMgntSpoolFromModel.RowStatus | (int)RowStatus.CHANGEATTR;
				}
				if (!CommonFunctions.IsSMgntSpoolSameShape(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
				{
					oSMgntSpoolFromModel.SpoolStatus += "形状修改;";
					oSMgntSpoolFromModel.RowStatus = oSMgntSpoolFromModel.RowStatus | (int)RowStatus.CHANGEATTR;
				}
				if (!CommonFunctions.IsSMgntSpoolSameNode(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
				{
					oSMgntSpoolFromModel.SpoolStatus += "节点修改;";
					oSMgntSpoolFromModel.RowStatus = oSMgntSpoolFromModel.RowStatus | (int)RowStatus.CHANGENODEFROM;
				}

				oSMgntSpoolFromModel.DrawNo = oSMgntSpoolFromDB.DrawNo;
				oSMgntSpoolFromModel.PageNo = oSMgntSpoolFromDB.PageNo;
			}
		}

		/// <summary>
		/// 比较数据库数据和模型数据间的差异，形成状态
		/// </summary>
		/// <param name="oSMgntSpoolFromDB"></param>
		/// <param name="oSMgntSpoolFromModel"></param>
		private void CheckRowAndSpoolStatus(ref SMgnt_SpoolInfo oSMgntSpoolFromDB, ref SMgnt_SpoolInfo oSMgntSpoolFromModel)
		{
			oSMgntSpoolFromDB.SpoolFromModel = oSMgntSpoolFromModel.SpoolFromModel;

			//检查模型数据和数据库数据间的差别
			oSMgntSpoolFromDB.SpoolStatus = "";
			if (!CommonFunctions.IsSMgntSpoolSameName(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
			{
				oSMgntSpoolFromDB.SpoolStatus += "名称修改;";
				oSMgntSpoolFromDB.RowStatus = oSMgntSpoolFromDB.RowStatus | (int)RowStatus.CHANGEATTR;
			}
			if (!CommonFunctions.IsSMgntSpoolSameAttr(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
			{
				oSMgntSpoolFromDB.SpoolStatus += "属性修改;";
				oSMgntSpoolFromDB.RowStatus = oSMgntSpoolFromDB.RowStatus | (int)RowStatus.CHANGEATTR;
			}
			if (!CommonFunctions.IsSMgntSpoolSameShape(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
			{
				oSMgntSpoolFromDB.SpoolStatus += "形状修改;";
				oSMgntSpoolFromDB.RowStatus = oSMgntSpoolFromDB.RowStatus | (int)RowStatus.CHANGEATTR;
			}
			if (oSMgntSpoolFromDB.Spool_oid != oSMgntSpoolFromModel.Spool_oid)
			{
				oSMgntSpoolFromDB.SpoolStatus += "修改oid;";
				oSMgntSpoolFromDB.RowStatus = oSMgntSpoolFromDB.RowStatus | (int)RowStatus.CHANGEOID;
			}
			if (!CommonFunctions.IsSMgntSpoolSameNode(oSMgntSpoolFromDB, oSMgntSpoolFromModel))
			{
				string sArea = comboBox_Area.SelectedText;
				string sBlock = comboBox_Block.SelectedText;
				string sMat = comboBox_ParentAss.SelectedText;
				string sMajor = comboBox_Major.SelectedText;

				oSMgntSpoolFromDB.SpoolStatus += "节点修改;";
				if (sArea == oSMgntSpoolFromDB.Zone && sBlock == oSMgntSpoolFromDB.Block && sMat == oSMgntSpoolFromDB.ParentAss && sMajor == oSMgntSpoolFromDB.Major)
					oSMgntSpoolFromDB.RowStatus = oSMgntSpoolFromDB.RowStatus | (int)RowStatus.CHANGENODETO;
				else
					oSMgntSpoolFromDB.RowStatus = oSMgntSpoolFromDB.RowStatus | (int)RowStatus.CHANGENODEFROM;
			}

			oSMgntSpoolFromModel.SpoolStatus = oSMgntSpoolFromDB.SpoolStatus;
			oSMgntSpoolFromModel.RowStatus = oSMgntSpoolFromDB.RowStatus;
			oSMgntSpoolFromModel.DrawNo = oSMgntSpoolFromDB.DrawNo;
			oSMgntSpoolFromModel.PageNo = oSMgntSpoolFromDB.PageNo;
			oSMgntSpoolFromModel.UpdateTime = oSMgntSpoolFromDB.UpdateTime;
			oSMgntSpoolFromModel.CreateBy = oSMgntSpoolFromDB.CreateBy;
		}

		/// <summary>
		/// 从当前选择集中，获取模型小票列表，并设置小票所在的区域，分段，材料节点
		/// </summary>
		private List<BusinessObject> GetSpoolsAndSetZoneBlockMatFromSelectSet()
		{
			//获取选择的小票
			List<BusinessObject> listOfSelectSpools = new List<BusinessObject>();
			foreach (var bo in ClientServiceProvider.SelectSet.SelectedObjects)
			{
				Spool oSpool = bo as Spool;
				if (oSpool != null)
					listOfSelectSpools.Add(oSpool);
				//排除空小票
				//if (oSpool != null && oSpool.AssemblyChildren.Count != 0)
				//	listOfSelectSpools.Add(oSpool);
				else
				{
					IAssembly oAss = bo as IAssembly;
					if (oAss != null)
					{
						foreach (var oAssChild in oAss.AssemblyChildren)
						{
							oSpool = oAssChild as Spool;
							if (oSpool != null)
								listOfSelectSpools.Add(oSpool);
						}
					}
				}
			}
			if (listOfSelectSpools.Count == 0)
			{
				MessageBox.Show("当前选择集中没有小票对象。");
				return null;
			}

			//判断小票是否来源于同一个节点
			if (!CommonFunctions.IsSpoolsInSameNode(listOfSelectSpools))
			{
				MessageBox.Show("当前选择的多个小票不属于同一个节点，请选择同一个节点下的小票。");
				return null;
			}

			//获取分区，分段，节点信息，获取模型小票加载到界面
			GetAreaBlockAndParentAssMap(listOfSelectSpools);

			//将m_AreaBlockAndParentAssMap中的分区分段节点填写到combo中，只有一条
			string sMajor = "";
			string sArea = "";
			string sBlock = "";
			ParentAss oParentAss = new ParentAss();
			CommonFunctions.GetAreaBloclAndParentAss(listOfSelectSpools[0], ref sMajor, ref sArea, ref sBlock, ref oParentAss);
			if (sArea == "" || sBlock == "")
			{
				MessageBox.Show("当前选择节点或小票所在的分段分区获取失败，可能原因为分区分段装配节点类型错误（分区：Pre-Assembly，分段：Pre-SubBlock）。");
				return null;
			}
			//comboBox_Area.Items.Clear();
			//comboBox_Block.Items.Clear();
			//comboBox_ParentAss.Items.Clear();

			//移除响应
			//comboBox_Area.SelectedIndexChanged -= ActionChangeArea;
			//comboBox_Block.SelectedIndexChanged -= ActionChangeBlock;
			comboBox_ParentAss.SelectedIndexChanged -= ActionChangeParentAss;

			//添加内容
			if (!comboBox_Major.Items.Contains(sMajor))
				comboBox_Major.Items.Add(sMajor);
			comboBox_Major.SelectedItem = sMajor;

			if (!comboBox_Area.Items.Contains(sArea))
				comboBox_Area.Items.Add(sArea);
			comboBox_Area.SelectedItem = sArea;

			if (!comboBox_Block.Items.Contains(sBlock))
				comboBox_Block.Items.Add(sBlock);
			comboBox_Block.SelectedItem = sBlock;

			if (!comboBox_ParentAss.Items.Contains(oParentAss.Name))
				comboBox_ParentAss.Items.Add(oParentAss.Name);
			comboBox_ParentAss.SelectedItem = oParentAss.Name;

			//添加响应
			//comboBox_Area.SelectedIndexChanged += ActionChangeArea;
			//comboBox_Block.SelectedIndexChanged += ActionChangeBlock;
			comboBox_ParentAss.SelectedIndexChanged += ActionChangeParentAss;

			return listOfSelectSpools;
		}

		/// <summary>
		/// 利用模型信息匹配数据库小票
		/// </summary>
		/// <param name="oSMgntSpoolFromModel"></param>
		/// <returns></returns>
		private SMgnt_SpoolInfo FindSMgntSpoolFromDB(SMgnt_SpoolInfo oSMgntSpoolFromModel)
		{
			using (SMgntEntities dbEneties = new SMgntEntities())
			{
				//利用oid匹配
				var result1 = dbEneties.SMgnt_SpoolInfo.Where(i => i.Spool_oid == oSMgntSpoolFromModel.Spool_oid);
				if (result1.Count() == 1)
				{
					return result1.First();
				}

				//先利用物理id进行匹配
				var result = dbEneties.SMgnt_SpoolInfo.Where(i => i.CalculateCode == oSMgntSpoolFromModel.CalculateCode);
				if (result.Count() == 1)
				{
					return result.First();
				}
				else if (result.Count() > 1)
				{
					MessageBox.Show($"更新小票{oSMgntSpoolFromModel.Name}时，从数据库检索到{result.Count()}个物理校验码一致的小票，联系管理员检查数据库。");
					return null;
				}

				//利用分区，分段，小票名字
				var result2 = dbEneties.SMgnt_SpoolInfo.Where(i =>
				i.Major == oSMgntSpoolFromModel.Major &&
				i.Zone == oSMgntSpoolFromModel.Zone &&
				i.Block == oSMgntSpoolFromModel.Block &&
				i.Name == oSMgntSpoolFromModel.Name &&
				i.IsValid != 1);
				if (result1.Count() == 1)
				{
					SMgnt_SpoolInfo oCurrSMgntSpoolFromDB = result1.First();

					//获取当前记录的小票模型是否存在，存在说明这条记录不匹配。否则认为是匹配的
					BusinessObject oBOOnSpool = CommonFunctions.GetBusinessObjectFromOid(oCurrSMgntSpoolFromDB.Spool_oid);
					if (oBOOnSpool != null)
						return null;
					else
						return result1.First();
				}
				else if (result2.Count() > 1)
				{
					MessageBox.Show($"更新小票{oSMgntSpoolFromModel.Name}时，从数据库检索到{result2.Count()}个分区、分段、小票名字一致的小票，请先更新相应记录。");
					return null;
				}

			}

			return null;
		}

		/// <summary>
		/// 当前选择行数据留白，只针对数据库数据，将状态修改未Null
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionChangeToNullData(object sender, EventArgs e)
		{
			using (SMgntEntities mdbContext = new SMgntEntities())
			{
				foreach (DataGridViewRow row in this.dataGridView_SpoolInfo.SelectedRows)
				{
					if (row.Visible == false)
						continue;

					string sSpoolOid = (string)row.Cells[SpoolInfoTable.Spool_oid].Value;
					string sSpoolName = row.Cells[SpoolInfoTable.NameDisplay].Value.ToString();

					//检查权限
					if (row.Cells[SpoolInfoTable.ResponseDisplay].Value.ToString() != m_sCurrentUser)
					{
						InfoLable.Text = $"当前用户无权限修改小票{sSpoolName}，跳过此小票...";
						continue;
					}

					// 查找当前小票
					var result = mdbContext.SMgnt_SpoolInfo.Where(i => i.Spool_oid == sSpoolOid);
					if (result.Count() == 0)
					{
						InfoLable.Text = $"小票{sSpoolName}还未在数据库存储，不能做留白处理，跳过此小票...";
						continue;
					}

					// 没编页码不能留白
					SMgnt_SpoolInfo oSMgntSpool = result.First();
					if (oSMgntSpool.PageNo == "")
					{
						InfoLable.Text = $"小票{sSpoolName}还未编排页码，不能做留白处理，跳过此小票...";
						continue;
					}

					//已留白页不能再留白
					if (oSMgntSpool.IsValid == 0)
					{
						InfoLable.Text = $"小票{sSpoolName}已留白，不能做留白处理，跳过此小票...";
						continue;
					}

					//新建空白页数据，插入数据库
					SMgnt_SpoolInfo oNullSMgntSpool = CommonFunctions.GeneralNullSMgntSpool(oSMgntSpool);
					oNullSMgntSpool.CreateBy = m_sCurrentUser;
					mdbContext.SMgnt_SpoolInfo.Add(oNullSMgntSpool);

					//删除当前数据
					mdbContext.SMgnt_SpoolInfo.Remove(oSMgntSpool);

					mdbContext.SaveChanges();

					//更新行数据
					row.Cells[SpoolInfoTable.Spool_oid].Value = oNullSMgntSpool.Spool_oid;
					row.Cells[SpoolInfoTable.SpoolFromModel].Value = null;
					row.Cells[SpoolInfoTable.Status].Value = RowStatus.UPDATE | RowStatus.NULL;
				}
			}

			RefreshSpoolDataGridView(true);
		}

		private void ActionHighLightObject(object sender, EventArgs e)
		{
			List<BusinessObject> listOfSelectedSpool = new List<BusinessObject>();
			foreach(DataGridViewRow row in dataGridView_SpoolInfo.SelectedRows)
			{
				if (row.Visible == false)
					continue;

				BusinessObject oBO = row.Cells[SpoolInfoTable.SpoolFromModel].Value as BusinessObject;
				if (oBO != null)
					listOfSelectedSpool.Add(oBO);
				else
				{
					BusinessObject oSpool = CommonFunctions.GetBusinessObjectFromOid(row.Cells[SpoolInfoTable.Spool_oid].Value.ToString());
					row.Cells[SpoolInfoTable.SpoolFromModel].Value = oSpool;

					if (oSpool != null)
						listOfSelectedSpool.Add(oSpool);
				}
			}

			CommonFunctions.FitObjects(listOfSelectedSpool);
		}

		private void ActionConfirmStatus(object sender, EventArgs e)
		{
			foreach(DataGridViewRow row in this.dataGridView_SpoolInfo.SelectedRows)
			{
				if (row.Visible == false)
					continue;

				//string sSpoolStatus = row.Cells[SpoolInfoTable.SpoolStatusDisplay].Value.ToString();
				//if (sSpoolStatus != "")
				//{
				string sSpoolOid = row.Cells[SpoolInfoTable.Spool_oid].Value.ToString();
				using (SMgntEntities dbEntities = new SMgntEntities())
				{
					var result = dbEntities.SMgnt_SpoolInfo.Where(i => i.Spool_oid == sSpoolOid);
					if (result.Count() != 0)
					{
						SMgnt_SpoolInfo oSMgntSpool = result.First();
						if (oSMgntSpool.SpoolStatus != "")
						{
							oSMgntSpool.SpoolStatus = "";
							row.Cells[SpoolInfoTable.SpoolStatusDisplay].Value = "";

							dbEntities.Entry(oSMgntSpool).State = System.Data.Entity.EntityState.Modified;

							dbEntities.SaveChanges();
						}

						row.Cells[SpoolInfoTable.SpoolStatusDisplay].Value = "";
					}
				}

				//}
			}
		}

		/// <summary>
		/// 过滤字段修改
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionFilterTextChange(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				FilterRows();
				dataGridView_SpoolInfo.Focus();
			}
		}

		/// <summary>
		/// 过滤显示行
		/// </summary>
		private void FilterRows()
		{
			string sText = textBox_Filter.Text;
			string sUpperText = sText.ToUpper();

			if (dataGridView_SpoolInfo.DataSource == null)
				return;

			CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView_SpoolInfo.DataSource];
			cm.SuspendBinding(); //挂起数据绑定

			//2020.12.24 改为CheckBox控制
			FilterColor filterColor = FilterColor.Undefine;
			if (checkBox_HideNoProblemItem.CheckState == CheckState.Checked)
				filterColor = filterColor | FilterColor.AntiqueWhite | FilterColor.Blue | FilterColor.Cyan | 
					FilterColor.LightBlue | FilterColor.Orange | FilterColor.Red | FilterColor.Yellow;

			foreach (DataGridViewRow row in this.dataGridView_SpoolInfo.Rows)
			{
				string sName = row.Cells[SpoolInfoTable.NameDisplay].Value.ToString();
				string sSpoolStatus = row.Cells[SpoolInfoTable.SpoolStatusDisplay].Value.ToString();
				string sUpperName = sName.ToUpper();
				if (sUpperName.Contains(sUpperText))
				{
					FilterColor currRowColor = GetRowColor(row);
					if (sSpoolStatus != "" ||
						filterColor == FilterColor.Undefine ||
						(currRowColor & filterColor) == currRowColor)
						row.Visible = true;
					else
						row.Visible = false;
				}
				else
					row.Visible = false;
			}

			cm.ResumeBinding(); //恢复数据绑定
		}

		private void ActionSelectAssNode(object sender, EventArgs e)
		{

			comboBox_SelectAss.EnableMore = true;

			int iIndex = 0;
			foreach(var item in ((ComboBox)comboBox_SelectAss).Items)
			{
				if (item.ToString() == "More ...")
				{
					((ComboBox)comboBox_SelectAss).SelectedIndex = iIndex;
					break;
				}

				iIndex++;
			}
		}

		private void SetStatusLabel()
		{
			spoolCountLabel.Text = $"总数({dataGridView_SpoolInfo.RowCount.ToString()})";

			if (dataGridView_SpoolInfo.SelectedRows.Count == 1)
			{
				connInfoLabel.Text = "连接(" + dataGridView_SpoolInfo.SelectedRows[0].Cells[SpoolInfoTable.StartInfoDisplay].Value.ToString() + " -> " + dataGridView_SpoolInfo.SelectedRows[0].Cells[SpoolInfoTable.EndInfoDisplay].Value.ToString() + ")";
			}
			else
			{
				connInfoLabel.Text = "";
			}
		}

		/// <summary>
		/// 显示色标面板
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionShowColorPanel(object sender, EventArgs e)
		{
			if (m_ColorPanel == null)
			{
				//色标面板
				m_ColorPanel = new ColorPanelForm();
				m_ColorPanel.FormClosing += M_ColorPanel_FormClosing;
				m_ColorPanel.FilteColor += M_ColorPanel_FilteColor;
				m_ColorPanel.VisibleChanged += M_ColorPanel_VisibleChanged;
				m_ColorPanel.Show(this);
			}
			m_ColorPanel.Visible = true;
			m_ColorPanel.Focus();
		}

		private void M_ColorPanel_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (e.CloseReason == CloseReason.UserClosing)
				e.Cancel = true;
			m_ColorPanel.Visible = false;
		}


		private void M_ColorPanel_FilteColor(object sender, EventArgs e)
		{
			//FilterRows();
		}
		private void M_ColorPanel_VisibleChanged(object sender, EventArgs e)
		{
			//FilterRows();
		}

		private void M_ColorPanel_FormClosed(object sender, FormClosedEventArgs e)
		{
			m_ColorPanel = null;
		}

		/// <summary>
		/// 显示或隐藏问题项
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ActionShowOrHideSpools(object sender, EventArgs e)
		{
			FilterRows();
		}

		private void ActionComboBox_Area_TextUpdate(object sender, EventArgs e)
		{
			if (comboBox_Major.SelectedItem == null)
				return;

			comboBox_Area.Items.Clear();
			string sUpperText = comboBox_Area.Text.ToUpper();

			List<string> listAreas = new List<string>();
			List<string> listInitAreas = m_MajorAndAreaMap[comboBox_Major.SelectedItem.ToString()].Keys.ToList();
			foreach(var it in listInitAreas)
			{
				if (it.ToUpper().Contains(sUpperText))
					listAreas.Add(it);
			}

			comboBox_Area.Items.AddRange(listAreas.ToArray());
			comboBox_Area.SelectionStart = comboBox_Area.Text.Length;
			Cursor = Cursors.Default;
			if (comboBox_Area.DroppedDown == true && listAreas.Count != 0)
				comboBox_Area.DroppedDown = false;
			comboBox_Area.DroppedDown = true;
		}

		private void ActionComboBox_Block_TextUpdate(object sender, EventArgs e)
		{
			if (comboBox_Area.SelectedItem == null)
				return;

			comboBox_Block.Items.Clear();
			string sUpperText = comboBox_Block.Text.ToUpper();

			List<string> listBlocks = new List<string>();
			List<string> listInitBlocks = m_MajorAndAreaMap[comboBox_Major.SelectedItem.ToString()][comboBox_Area.SelectedItem.ToString()].Keys.ToList();
			foreach (var it in listInitBlocks)
			{
				if (it.ToUpper().Contains(sUpperText))
					listBlocks.Add(it);
			}

			comboBox_Block.Items.AddRange(listBlocks.ToArray());
			comboBox_Block.SelectionStart = comboBox_Block.Text.Length;
			Cursor = Cursors.Default;
			if (comboBox_Block.DroppedDown == true && listBlocks.Count != 0)
				comboBox_Block.DroppedDown = false;
			comboBox_Block.DroppedDown = true;

		}

		private void ActionComboBox_ParentAss_TextUpdate(object sender, EventArgs e)
		{
			if (comboBox_Block.SelectedItem == null)
				return;

			comboBox_ParentAss.Items.Clear();
			string sUpperText = comboBox_ParentAss.Text.ToUpper();

			List<string> listMats = new List<string>();
			List<ParentAss> listInitMats = m_MajorAndAreaMap[comboBox_Major.SelectedItem.ToString()][comboBox_Area.SelectedItem.ToString()][comboBox_Block.SelectedItem.ToString()];
			foreach (var it in listInitMats)
			{
				if (it.Name.ToUpper().Contains(sUpperText))
					listMats.Add(it.Name);
			}

			comboBox_ParentAss.Items.AddRange(listMats.ToArray());
			comboBox_ParentAss.SelectionStart = comboBox_ParentAss.Text.Length;
			Cursor = Cursors.Default;
			if (comboBox_ParentAss.DroppedDown == true && listMats.Count != 0)
				comboBox_ParentAss.DroppedDown = false;
			comboBox_ParentAss.DroppedDown = true;
		}

		private void menuItem_ClearSelection_Click(object sender, EventArgs e)
		{
			dataGridView_SpoolInfo.ClearSelection();
		}
	}
}
